#include "device.h"

device obj_device = {
    "obj",
};
int list_dev_init()
{
    list_init(&obj_device.device_list);
    return true;
}
device *device_find(char *name)
{
    device *dev = NULL;
    list *p = &obj_device.device_list;
    if (p->next == NULL)
        return NULL;
    while (p->next != &obj_device.device_list)
    {
        dev = (device *)container_of(p->next, device, device_list);
        if (dev != NULL)
        {
            if (!strncmp(name, dev->name, NAME_MAXLEN)) // �ҵ��˶�Ӧ���豸
            {
                return dev;
            }
        }
        p = p->next;
    }
    return NULL;
}
int register_device(device *dev)
{
    if (dev != NULL)
    {
        list_init(&dev->device_list);
        list_insert_after(&obj_device.device_list, &dev->device_list);
    }
    else
        return -1;
    return 0;
}
int delete_device(device *dev)
{
    if (dev != NULL)
        list_remove(&dev->device_list);
    else
        return -1;
    return 0;
}
int Get_device_num()
{
    return list_len(&obj_device.device_list);
}
int device_init(device *dev)
{
    if (dev == NULL)
        return -1;
    if (dev->init_flag == true)
        return -1;
    if (dev->init == NULL)
        return -1;
    if (dev->init(dev) == 0)
    {
        dev->init_flag = true; // ��ʼ�����
        return 0;
    }
    else
        return -1;
}
int device_open(device *dev, int flag)
{
    int sta;
    if (dev == NULL)
        return -1;
    if (dev->init_flag == false)
    {
        if (device_init(dev) == -1)
            return -1;
        dev->init_flag = true;
    }
    if (dev->open == NULL)
        return -1;
    sta = dev->open(dev, flag);
    return sta;
}
int device_close(device *dev)
{
    int sta;
    if (dev == NULL)
        return -1;
    dev->init_flag = false;
    if (dev->close == NULL)
        return -1;
    sta = dev->close(dev);
    return sta;
}
int device_control(device *dev, uint8_t cmd, void *args)
{
    int sta;
    if (dev == NULL)
        return -1;
    if (dev->init_flag == false)
        return -1;
    if (dev->contorl == NULL)
        return -1;
    sta = dev->contorl(dev, cmd, args);
    return sta;
}
int device_read(device *dev, int offest, uint8_t *buf, int len)
{
    int sta;
    if (dev == NULL)
        return -1;
    if (dev->init_flag == false)
        return -1;
    if (dev->read == NULL)
        return -1;
    sta = dev->read(dev, offest, buf, len);
    return sta;
}
int device_write(device *dev, int offest, uint8_t *buf, int len)
{
    if (dev == NULL)
        return -1;
    if (dev->init_flag == false)
        return -1;
    if (dev->write == NULL)
        return -1;
    return dev->write(dev, offest, buf, len);
}

static int board_Start(void)
{
    return 0;
}
BOARD_START_EXPORT(board_Start);

static int board_End(void)
{
    return 0;
}
BOARD_END_EXPORT(board_End);

static int bsp_End(void)
{
    return 0;
}
BSP_END_EXPORT(bsp_End);

static int app_end(void)
{
    return 0;
}
APP_END_EXPORT(app_end);

int board_Init(void)
{
    const auto_init *cmd_ptr;
    for (cmd_ptr = &cmd_fn_board_Start; cmd_ptr < &cmd_fn_board_End; cmd_ptr++)
    {
        (*cmd_ptr)();
    }
    return 1;
}

int bsp_Init(void)
{
    const auto_init *cmd_ptr;
    for (cmd_ptr = &cmd_fn_board_End; cmd_ptr < &cmd_fn_bsp_End; cmd_ptr++)
    {
        (*cmd_ptr)();
    }
    return 1;
}
int app_Init(void)
{
    const auto_init *cmd_ptr;
    for (cmd_ptr = &cmd_fn_bsp_End; cmd_ptr < &cmd_fn_app_end; cmd_ptr++)
    {
        (*cmd_ptr)();
    }
    return 1;
}

